package com.xingzhi.shortvideosharingplatform.controller;

import com.xingzhi.shortvideosharingplatform.common.Result;
import com.xingzhi.shortvideosharingplatform.dto.CommentDTO;
import com.xingzhi.shortvideosharingplatform.dto.CommentTreeDTO;
import com.xingzhi.shortvideosharingplatform.service.CommentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/comments")
public class CommentController {

    @Autowired
    private CommentService commentService;

    /**
     * 创建评论
     * @param commentDTO 评论信息
     * @return 评论ID
     */
    @PostMapping
    public Result<Long> addComment(@RequestBody CommentDTO commentDTO) {
        if (commentDTO == null || commentDTO.getUserId() == null || commentDTO.getUserId().equals(0L) || 
            commentDTO.getVideoId() == null || commentDTO.getVideoId().equals(0L) || commentDTO.getContent() == null) {
            return Result.error(400, "参数不完整");
        }
        
        try {
            Long commentId = commentService.addComment(commentDTO);
            return Result.success(commentId, "评论成功");
        } catch (Exception e) {
            log.error("创建评论失败", e);
            return Result.error(500, "评论失败: " + e.getMessage());
        }
    }
    
    /**
     * 获取视频的评论树
     * @param videoId 视频ID
     * @return 评论树列表
     */
    @GetMapping("/tree/{videoId}")
    public Result<List<CommentTreeDTO>> getCommentTree(@PathVariable Long videoId) {
        if (videoId == null || videoId <= 0) {
            return Result.error(400, "视频ID不合法");
        }
        
        try {
            List<CommentTreeDTO> commentTree = commentService.getCommentTreeByVideoId(videoId);
            return Result.success(commentTree);
        } catch (Exception e) {
            log.error("获取评论树失败", e);
            return Result.error(500, "获取评论失败: " + e.getMessage());
        }
    }
    
    /**
     * 点赞评论
     * @param commentId 评论ID
     * @return 操作结果
     */
    @PostMapping("/{commentId}/like")
    public Result<Boolean> likeComment(@PathVariable Long commentId) {
        if (commentId == null || commentId <= 0) {
            return Result.error(400, "评论ID不合法");
        }
        
        try {
            boolean success = commentService.incrementCommentLikeCount(commentId);
            if (success) {
                return Result.success(true, "点赞成功");
            } else {
                return Result.error(500, "点赞失败");
            }
        } catch (Exception e) {
            log.error("点赞评论失败", e);
            return Result.error(500, "点赞失败: " + e.getMessage());
        }
    }
    
    /**
     * 取消点赞评论
     * @param commentId 评论ID
     * @return 操作结果
     */
    @DeleteMapping("/{commentId}/like")
    public Result<Boolean> unlikeComment(@PathVariable Long commentId) {
        if (commentId == null || commentId <= 0) {
            return Result.error(400, "评论ID不合法");
        }
        
        try {
            boolean success = commentService.decrementCommentLikeCount(commentId);
            if (success) {
                return Result.success(true, "取消点赞成功");
            } else {
                return Result.error(500, "取消点赞失败");
            }
        } catch (Exception e) {
            log.error("取消点赞评论失败", e);
            return Result.error(500, "取消点赞失败: " + e.getMessage());
        }
    }
} 